package com.ybkj.o2o.zhuanxian.controller.res;

import com.ybkj.o2o.zhuanxian.common.DataGrid;
import com.ybkj.o2o.zhuanxian.common.error.Error;
import com.ybkj.o2o.zhuanxian.common.error.ErrorConstantV4;
import com.ybkj.o2o.zhuanxian.common.error.ResultTransport;
import com.ybkj.o2o.zhuanxian.entity.Car;
import com.ybkj.o2o.zhuanxian.entity.Line;
import com.ybkj.o2o.zhuanxian.manager.CarManager;
import com.ybkj.o2o.zhuanxian.manager.LineManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 专线车辆 .
 * @author haoduoduo
 *
 */
@Controller
@RequestMapping("api/car")
public class CarController {
  private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CarController.class);
  @Autowired
  private CarManager carManager;
  @Autowired
  private LineManager lineManager;
  
  
  /**
   * 创建专线车辆 .
   * @param lineId              线路id
   * @param lineName            线路名称
   * @param companyId           公司id    
   * @param companyName         公司名称     
   * @param carName             车辆名称
   * @param carNumber           车牌号
   * @param drivingDistance     司机距离
   * @param buyTime             时间
   * @param employId            司机id
   * @param employName          司机名称
   * @param limitLine           线路
   * @param insuranceTime       保险时间   
   * @param overhaulTime        检修时间
   * @param picture             照片
   * @param provincial          省
   * @param city                城市
   * @param county              县
   * @param address             地址
   * @param ridepeopleNumber    承载人数
   * @param appKey              区分系统
   * @return ResultTransport
   */
  @RequestMapping("createCar")
  @ResponseBody
  public ResultTransport createCar(
      @RequestParam("lineId") Long[] lineId,
      @RequestParam(required = false, value = "lineName") String lineName,
      @RequestParam("companyId") Long companyId, 
      @RequestParam("companyName") String companyName,
      @RequestParam("carName") String carName, 
      @RequestParam("carNumber") String carNumber,
      @RequestParam(required = false, value = "drivingDistance") Double drivingDistance,
      @RequestParam(required = false, value = "buyTime") Long buyTime, 
      @RequestParam(required = false, value = "employId") Long employId,
      @RequestParam(required = false, value = "employName") String employName, 
      @RequestParam(required = false, value = "limitLine") Integer limitLine, 
      @RequestParam(required = false, value = "insuranceTime") Long insuranceTime, 
      @RequestParam(required = false, value = "overhaulTime") Long overhaulTime, 
      @RequestParam(required = false, value = "picture") String picture,
      @RequestParam(required = false, value = "provincial") String provincial, 
      @RequestParam(required = false, value = "city") String city, 
      @RequestParam(required = false, value = "county") String county,
      @RequestParam(required = false, value = "address") String address,
      @RequestParam("ridepeopleNumber") Integer ridepeopleNumber,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {

      Car car = new Car();
      List<Car> findBycarNumber = carManager.findBycarNumber(carNumber, appKey);
      if (!CollectionUtils.isEmpty(findBycarNumber)) {
        return ResultTransport
            .getErrorBy(new Error(ErrorConstantV4.PARAM_ERROR.getCode(), "车牌号已存在"));
      }
      List<Line> lines = new ArrayList<Line>();
      if (lineId != null) {
        for (Long lid : lineId) {
          Line line = lineManager.findById(lid, appKey);
          if (line == null) {
            return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_LINE_NOT_EXISTS);
          }
          lines.add(line);
        }
      }
      car.setUpdated(new Date());
      car.setCreated(new Date());
      car.setAddress(address);
      car.setAppKey(appKey);
      car.setBuyTime(buyTime != null ? new Date(buyTime) : null);
      car.setCarName(carName);
      car.setCarNumber(carNumber);
      car.setCity(city);
      car.setCompanyId(companyId);
      car.setCompanyName(companyName);
      car.setCounty(county);
      car.setDrivingDistance(drivingDistance);
      car.setEmployId(employId);
      car.setEmployName(employName);
      car.setInsuranceTime(insuranceTime != null ? new Date(insuranceTime) : null);
      car.setLimitLine(limitLine);
      car.setOverhaulTime(overhaulTime != null ? new Date(overhaulTime) : null);
      car.setPicture(picture);
      car.setProvincial(provincial);
      car.setRidepeopleNumber(ridepeopleNumber);
      car.setStatus(Car.CAR_FREE);
      car.setVersion(1L);
      car.setLines(lines);
      car = carManager.create(car);

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(car);

      return response;
    } catch (Exception ex) {
      logger.error("createCar:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }

  }
  
  /**
   * 创建车辆(新版)
   * @return
   */
  @RequestMapping(value="createNewCar", method = RequestMethod.POST)
  @ResponseBody
  public ResultTransport createNewCar(
      @RequestParam("lineId") Long lineId,
      @RequestParam(required = false, value = "lineName") String lineName,
      @RequestParam("companyId") Long companyId, 
      @RequestParam("companyName") String companyName,
      @RequestParam("carName") String carName, 
      @RequestParam("carNumber") String carNumber,
      @RequestParam(required = false, value = "drivingDistance") Double drivingDistance,
      @RequestParam(required = false, value = "buyTime") Long buyTime,
      @RequestParam(required = false, value = "employId") Long employId,
      @RequestParam(required = false, value = "employName") String employName, 
      @RequestParam(required = false, value = "insuranceTime") Long insuranceTime, 
      @RequestParam(required = false, value = "overhaulTime") Long overhaulTime, 
      @RequestParam(required = false, value = "picture") String picture,
      @RequestParam(required = false, value = "provincial") String provincial, 
      @RequestParam(required = false, value = "city") String city, 
      @RequestParam(required = false, value = "county") String county,
      @RequestParam(required = false, value = "address") String address,
      @RequestParam("ridepeopleNumber") Integer ridepeopleNumber,
      @RequestParam(required = false, value = "plateColor")String plateColor,
      @RequestParam(required = false, value = "brand")String brand,
      @RequestParam(required = false, value = "vehicleType")String vehicleType,
      @RequestParam(required = false, value = "vehicleModel")String vehicleModel,
      @RequestParam(required = false, value = "owner")String owner,
      @RequestParam(required = false, value = "bodyColor")String bodyColor,
      @RequestParam(required = false, value = "engineNo")String engineNo,
      @RequestParam(required = false, value = "vin")String vin,
      @RequestParam(required = false, value = "fuel")String fuel,
      @RequestParam(required = false, value = "permitted")String permitted,
      @RequestParam(required = false, value = "registrationTime")Long registrationTime,
      @RequestParam(required = false, value = "inspectioStatus")Integer inspectioStatus,
      @RequestParam(required = false, value = "rateType")Integer rateType,
      @RequestParam(required = false, value = "displacement")String displacement,
      @RequestParam(required = false, value = "maintenanceStatus")Integer maintenanceStatus,
      @RequestParam(required = false, value = "taxiNo")String taxiNo,
      @RequestParam(required = false, value = "taxiAgency")String taxiAgency,
      @RequestParam(required = false, value = "taxiRegion")String taxiRegion,
      @RequestParam(required = false, value = "validTime")Long validTime,
      @RequestParam(required = false, value = "invalidTime")Long invalidTime,
      @RequestParam(required = false, value = "taxiRegisterTime")Long taxiRegisterTime,
      @RequestParam(required = false, value = "deviceSerial")String deviceSerial,
      @RequestParam(required = false, value = "deviceBrand")String deviceBrand,
      @RequestParam(required = false, value = "deviceModel")String deviceModel,
      @RequestParam(required = false, value = "deviceInstallTime")Long deviceInstallTime,
      @RequestParam(required = false, value = "insurer")String insurer,
      @RequestParam(required = false, value = "insuranceNo")String insuranceNo,
      @RequestParam(required = false, value = "insType")String insType,
      @RequestParam(required = false, value = "insMoney")Double insMoney,
      @RequestParam(required = false, value = "insValidTime")Long insValidTime,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      
      Car oldCar = carManager.findByNumber(carNumber, appKey);
      if (oldCar != null) {
        return ResultTransport
            .getErrorBy(new Error(ErrorConstantV4.PARAM_ERROR.getCode(), "车牌号已存在"));
      }
      
      List<Line> lines = new ArrayList<Line>();
      if (lineId != null) {
          Line line = lineManager.findById(lineId, appKey);
          if (line == null) {
            return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_LINE_NOT_EXISTS);
          }
          lines.add(line);
      }
      Car car = new Car();
      car.setAddress(address);
      car.setAppKey(appKey);
      if (buyTime != null) {
        car.setBuyTime(new Date(buyTime));
      }
      car.setCarName(carName);
      car.setCarNumber(carNumber);
      car.setCity(city);
      car.setCompanyId(companyId);
      car.setCompanyName(companyName);
      car.setCounty(county);
      car.setDrivingDistance(drivingDistance);
      car.setEmployId(employId);
      car.setEmployName(employName);
      car.setInsuranceTime(insuranceTime != null ? new Date(insuranceTime) : null);
      car.setOverhaulTime(overhaulTime != null ? new Date(overhaulTime) : null);
      car.setPicture(picture);
      
      if (StringUtils.isNotBlank(provincial)) {
        String[] split = provincial.split("-");
        car.setProvincial(split[0]);
        car.setCity(split[1]);
        car.setCounty(split[2]);
      }
      
      car.setRidepeopleNumber(ridepeopleNumber);
      car.setStatus(Car.CAR_FREE);
      car.setLines(lines);

      car.setPlateColor(plateColor);
      car.setBrand(brand);
      car.setVehicleType(vehicleType);
      car.setVehicleModel(vehicleModel);
      car.setOwner(owner);
      car.setBodyColor(bodyColor);
      car.setEngineNo(engineNo);
      car.setVin(vin);
      car.setFuel(fuel);
      car.setPermitted(permitted);
      car.setInsurer(insurer);
      car.setInsuranceNo(insuranceNo);
      car.setInsType(insType);
      car.setInsMoney(insMoney);
      if (null != insValidTime) {
        car.setInsValidTime(new Date(insValidTime));
      }
      if (null != registrationTime) {
        car.setRegistrationTime(new Date(registrationTime));
      }
      car.setInspectioStatus(inspectioStatus);
      car.setRateType(rateType);
      car.setDisplacement(displacement);
      car.setMaintenanceStatus(maintenanceStatus);
      car.setTaxiNo(taxiNo);
      car.setTaxiAgency(taxiAgency);
      car.setTaxiRegion(taxiRegion);
      if (null != validTime) {
        car.setValidTime(new Date(validTime));
      }
      if (null != invalidTime) {
        car.setInvalidTime(new Date(invalidTime));
      }
      if (null != taxiRegisterTime) {
        car.setTaxiRegisterTime(new Date(taxiRegisterTime));
      }
      car.setDeviceSerial(deviceSerial);
      car.setDeviceBrand(deviceBrand);
      car.setDeviceModel(deviceModel);
      car.setAddress(address);
      if (null != deviceInstallTime) {
        car.setDeviceInstallTime(new Date(deviceInstallTime));
      }
      
      car = carManager.create(car);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(car);
      
      return response;
    } catch (Exception ex) {
      logger.error("createCar:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    
  }

  /**
   * 修改专线车辆 .
   * @param lineId              线路id
   * @param lineName            线路名称
   * @param companyId           公司id    
   * @param companyName         公司名称     
   * @param carName             车辆名称
   * @param carNumber           车牌号
   * @param drivingDistance     司机距离
   * @param buyTime             时间
   * @param employId            司机id
   * @param employName          司机名称
   * @param limitLine           线路
   * @param insuranceTime       保险时间   
   * @param overhaulTime        检修时间
   * @param picture             照片
   * @param provincial          省
   * @param city                城市
   * @param county              县
   * @param address             地址
   * @param ridepeopleNumber    承载人数
   * @param appKey              区分系统
   * @return ResultTransport
   */
  @RequestMapping("update")
  @ResponseBody
  public ResultTransport update(
      @RequestParam("carId") Long carId, 
      @RequestParam(required = false, value = "lineId") Long[] lineId,
      @RequestParam(required = false, value = "lineName") String lineName,
      @RequestParam(required = false, value = "companyId") Long companyId, 
      @RequestParam(required = false, value = "companyName") String companyName, 
      @RequestParam(required = false, value = "carName") String carName,
      @RequestParam(required = false, value = "carNumber") String carNumber, 
      @RequestParam(required = false, value = "status") Integer status, 
      @RequestParam(required = false, value = "drivingDistance") Double drivingDistance, 
      @RequestParam(required = false, value = "buyTime") Long buyTime,
      @RequestParam(required = false, value = "employId") Long employId, 
      @RequestParam(required = false, value = "employName") String employName,
      @RequestParam(required = false, value = "limitLine") Integer limitLine,
      @RequestParam(required = false, value = "insuranceTime") Long insuranceTime,
      @RequestParam(required = false, value = "overhaulTime") Long overhaulTime,
      @RequestParam(required = false, value = "picture") String picture,
      @RequestParam(required = false, value = "provincial") String provincial, 
      @RequestParam(required = false, value = "city") String city, 
      @RequestParam(required = false, value = "county") String county,
      @RequestParam(required = false, value = "address") String address, 
      @RequestParam(required = false, value = "ridepeopleNumber") Integer ridepeopleNumber,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      Car car = carManager.findOne(carId, appKey);
      if (car == null) {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_CAR_NOT_EXISTS);
      }
      List<Car> findBycarNumber = carManager.findBycarNumber(carNumber, appKey);
      if (!CollectionUtils.isEmpty(findBycarNumber)) {
        for (Car c : findBycarNumber) {
          if (!c.getId().equals(car.getId())) {
            return ResultTransport.getErrorBy(new Error(ErrorConstantV4.PARAM_ERROR.getCode(),
                "车牌号已存在"));
          }
        }
      }

      List<Line> lines = new ArrayList<Line>();
      if (lineId != null) {
        for (Long lid : lineId) {
          Line line = lineManager.findById(lid, appKey);
          if (line == null) {
            return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_LINE_NOT_EXISTS);
          }
          lines.add(line);
        }

      }
      car.setCompanyId(companyId != null ? companyId : car.getCompanyId());
      car.setCompanyName(StringUtils.isNotBlank(companyName) ? companyName : car.getCompanyName());
      car.setCarName(StringUtils.isNotBlank(carName) ? carName : car.getCarName());
      car.setCarNumber(StringUtils.isNotBlank(carNumber) ? carNumber : car.getCarNumber());
      car.setStatus(status != null ? status : car.getStatus());
      car.setDrivingDistance(drivingDistance != null ? drivingDistance : car.getDrivingDistance());
      car.setBuyTime(buyTime != null ? new Date(buyTime) : car.getBuyTime());
      car.setEmployId(employId != null ? employId : car.getEmployId());
      car.setEmployName(StringUtils.isNotBlank(employName) ? employName : car.getEmployName());
      car.setLimitLine(limitLine != null ? limitLine : car.getLimitLine());
      car.setInsuranceTime(
          insuranceTime != null ? new Date(insuranceTime) : car.getInsuranceTime());
      car.setOverhaulTime(overhaulTime != null ? new Date(overhaulTime) : car.getOverhaulTime());
      car.setPicture(StringUtils.isNotBlank(picture) ? picture : car.getPicture());
      car.setProvincial(StringUtils.isNoneBlank(provincial) ? provincial : car.getProvincial());
      car.setCity(StringUtils.isNoneBlank(city) ? city : car.getCity());
      car.setCounty(StringUtils.isNotBlank(county) ? county : car.getCounty());
      car.setAddress(StringUtils.isNotBlank(address) ? address : car.getAddress());
      car.setRidepeopleNumber(
          ridepeopleNumber != null ? ridepeopleNumber : car.getRidepeopleNumber());
      car.setLines(lines != null && lines.size() > 0 ? lines : car.getLines());
      carManager.update(car);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return response;
    } catch (Exception ex) {
      logger.error("update:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }

  }
  @RequestMapping("updatezhuanxiancar")
  @ResponseBody
  public ResultTransport updatezhuanxiancar(
      @RequestParam("carId") Long carId, 
      @RequestParam(required = false, value = "lineId") Long[] lineId,
      @RequestParam(required = false, value = "lineName") String lineName,
      @RequestParam(required = false, value = "companyId") Long companyId, 
      @RequestParam(required = false, value = "companyName") String companyName, 
      @RequestParam(required = false, value = "carName") String carName,
      @RequestParam(required = false, value = "carNumber") String carNumber, 
      @RequestParam(required = false, value = "status") Integer status, 
      @RequestParam(required = false, value = "drivingDistance") Double drivingDistance, 
      @RequestParam(required = false, value = "buyTime") Long buyTime,
      @RequestParam(required = false, value = "employId") Long employId, 
      @RequestParam(required = false, value = "employName") String employName,
      @RequestParam(required = false, value = "limitLine") Integer limitLine,
      @RequestParam(required = false, value = "insuranceTime") Long insuranceTime,
      @RequestParam(required = false, value = "overhaulTime") Long overhaulTime,
      @RequestParam(required = false, value = "picture") String picture,
      @RequestParam(required = false, value = "provincial") String provincial, 
      @RequestParam(required = false, value = "city") String city, 
      @RequestParam(required = false, value = "county") String county,
      @RequestParam(required = false, value = "address") String address, 
      @RequestParam(required = false, value = "ridepeopleNumber") Integer ridepeopleNumber,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      Car car = carManager.findOne(carId, appKey);
      if (car == null) {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_CAR_NOT_EXISTS);
      }
      List<Car> findBycarNumber = carManager.findBycarNumber(carNumber, appKey);
      if (!CollectionUtils.isEmpty(findBycarNumber)) {
        for (Car c : findBycarNumber) {
          if (!c.getId().equals(car.getId())) {
            return ResultTransport.getErrorBy(new Error(ErrorConstantV4.PARAM_ERROR.getCode(),
                "车牌号已存在"));
          }
        }
      }

      List<Line> lines = new ArrayList<Line>();
      if (lineId != null) {
        for (Long lid : lineId) {
          Line line = lineManager.findById(lid, appKey);
          if (line == null) {
            return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_LINE_NOT_EXISTS);
          }
          lines.add(line);
        }

      }
      car.setCompanyId(companyId != null ? companyId : car.getCompanyId());
      car.setCompanyName(StringUtils.isNotBlank(companyName) ? companyName : car.getCompanyName());
      car.setCarName(StringUtils.isNotBlank(carName) ? carName : car.getCarName());
      car.setCarNumber(StringUtils.isNotBlank(carNumber) ? carNumber : car.getCarNumber());
      car.setStatus(status != null ? status : car.getStatus());
      car.setDrivingDistance(drivingDistance != null ? drivingDistance : car.getDrivingDistance());
      car.setBuyTime(buyTime != null ? new Date(buyTime) : car.getBuyTime());
      car.setEmployId(employId != null ? employId : car.getEmployId());
      car.setEmployName(StringUtils.isNotBlank(employName) ? employName : car.getEmployName());
      car.setLimitLine(limitLine != null ? limitLine : car.getLimitLine());
      car.setInsuranceTime(
          insuranceTime != null ? new Date(insuranceTime) : car.getInsuranceTime());
      car.setOverhaulTime(overhaulTime != null ? new Date(overhaulTime) : car.getOverhaulTime());
      car.setPicture(StringUtils.isNotBlank(picture) ? picture : car.getPicture());
      car.setProvincial(StringUtils.isNoneBlank(provincial) ? provincial : car.getProvincial());
      car.setCity(StringUtils.isNoneBlank(city) ? city : car.getCity());
      car.setCounty(StringUtils.isNotBlank(county) ? county : car.getCounty());
      car.setAddress(StringUtils.isNotBlank(address) ? address : car.getAddress());
      car.setRidepeopleNumber(
          ridepeopleNumber != null ? ridepeopleNumber : car.getRidepeopleNumber());
      car.setLines(lines != null && lines.size() > 0 ? lines : car.getLines());
      carManager.update(car);
      if(employId==null){
    	  System.out.println(car.getEmployId());
    	  System.out.println(car.getEmployName());
    	  carManager.updateEmployIdToNull(car);
      }
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return response;
    } catch (Exception ex) {
      logger.error("update:{}",ex.getMessage());
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }

  }
  @RequestMapping(value="updateZxCar", method=RequestMethod.POST)
  @ResponseBody
  public ResultTransport updateZxCar(
      @RequestParam("id") Long carId, 
      @RequestParam(required = false, value = "lineId") Long lineId,
      @RequestParam(required = false, value = "lineName") String lineName,
      @RequestParam(required = false, value = "companyId") Long companyId, 
      @RequestParam(required = false, value = "companyName") String companyName, 
      @RequestParam(required = false, value = "carName") String carName,
      @RequestParam(required = false, value = "carNumber") String carNumber, 
      @RequestParam(required = false, value = "status") Integer status, 
      @RequestParam(required = false, value = "drivingDistance") Double drivingDistance, 
      @RequestParam(required = false, value = "buyTime") Long buyTime,
      @RequestParam(required = false, value = "employId") Long employId, 
      @RequestParam(required = false, value = "employName") String employName,
      @RequestParam(required = false, value = "limitLine") Integer limitLine,
      @RequestParam(required = false, value = "insuranceTime") Long insuranceTime,
      @RequestParam(required = false, value = "overhaulTime") Long overhaulTime,
      @RequestParam(required = false, value = "picture") String picture,
      @RequestParam(required = false, value = "provincial") String provincial, 
      @RequestParam(required = false, value = "city") String city, 
      @RequestParam(required = false, value = "county") String county,
      @RequestParam(required = false, value = "address") String address, 
      @RequestParam(required = false, value = "ridepeopleNumber") Integer ridepeopleNumber,
      @RequestParam(required = false, value = "plateColor")String plateColor,
      @RequestParam(required = false, value = "brand")String brand,
      @RequestParam(required = false, value = "vehicleType")String vehicleType,
      @RequestParam(required = false, value = "vehicleModel")String vehicleModel,
      @RequestParam(required = false, value = "owner")String owner,
      @RequestParam(required = false, value = "bodyColor")String bodyColor,
      @RequestParam(required = false, value = "engineNo")String engineNo,
      @RequestParam(required = false, value = "vin")String vin,
      @RequestParam(required = false, value = "fuel")String fuel,
      @RequestParam(required = false, value = "permitted")String permitted,
      @RequestParam(required = false, value = "registrationTime")Long registrationTime,
      @RequestParam(required = false, value = "inspectioStatus")Integer inspectioStatus,
      @RequestParam(required = false, value = "rateType")Integer rateType,
      @RequestParam(required = false, value = "displacement")String displacement,
      @RequestParam(required = false, value = "maintenanceStatus")Integer maintenanceStatus,
      @RequestParam(required = false, value = "taxiNo")String taxiNo,
      @RequestParam(required = false, value = "taxiAgency")String taxiAgency,
      @RequestParam(required = false, value = "taxiRegion")String taxiRegion,
      @RequestParam(required = false, value = "validTime")Long validTime,
      @RequestParam(required = false, value = "invalidTime")Long invalidTime,
      @RequestParam(required = false, value = "taxiRegisterTime")Long taxiRegisterTime,
      @RequestParam(required = false, value = "deviceSerial")String deviceSerial,
      @RequestParam(required = false, value = "deviceBrand")String deviceBrand,
      @RequestParam(required = false, value = "deviceModel")String deviceModel,
      @RequestParam(required = false, value = "deviceInstallTime")Long deviceInstallTime,
      @RequestParam(required = false, value = "insurer")String insurer,
      @RequestParam(required = false, value = "insuranceNo")String insuranceNo,
      @RequestParam(required = false, value = "insType")String insType,
      @RequestParam(required = false, value = "insMoney")Double insMoney,
      @RequestParam(required = false, value = "insValidTime")Long insValidTime,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      Car car = carManager.findOne(carId, appKey);
      if (car == null) {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_CAR_NOT_EXISTS);
      }
      
      Car oldCar = carManager.findByNumber(carNumber, appKey);
      if (oldCar != null && !oldCar.getId().equals(carId)) {
        return ResultTransport
            .getErrorBy(new Error(ErrorConstantV4.PARAM_ERROR.getCode(), "车牌号已存在"));
      }
      List<Line> lines = new ArrayList<Line>();
      if (lineId != null) {
          Line line = lineManager.findById(lineId, appKey);
          if (line == null) {
            return ResultTransport.getErrorBy(ErrorConstantV4.ZHUANXIAN_LINE_NOT_EXISTS);
          }
          lines.add(line);
      }
      car.setCompanyId(companyId != null ? companyId : car.getCompanyId());
      car.setCompanyName(StringUtils.isNotBlank(companyName) ? companyName : car.getCompanyName());
      car.setCarName(StringUtils.isNotBlank(carName) ? carName : car.getCarName());
      car.setCarNumber(StringUtils.isNotBlank(carNumber) ? carNumber : car.getCarNumber());
      car.setStatus(status != null ? status : car.getStatus());
      car.setDrivingDistance(drivingDistance != null ? drivingDistance : car.getDrivingDistance());
      car.setBuyTime(buyTime != null ? new Date(buyTime) : car.getBuyTime());
      car.setEmployId(employId != null ? employId : null);
      car.setEmployName(StringUtils.isNotBlank(employName) ? employName : "");
      car.setLimitLine(limitLine != null ? limitLine : car.getLimitLine());
      car.setInsuranceTime(
          insuranceTime != null ? new Date(insuranceTime) : car.getInsuranceTime());
      car.setOverhaulTime(overhaulTime != null ? new Date(overhaulTime) : car.getOverhaulTime());
      car.setPicture(StringUtils.isNotBlank(picture) ? picture : car.getPicture());
      car.setProvincial(StringUtils.isNoneBlank(provincial) ? provincial : car.getProvincial());
      car.setCity(StringUtils.isNoneBlank(city) ? city : car.getCity());
      car.setCounty(StringUtils.isNotBlank(county) ? county : car.getCounty());
      car.setAddress(StringUtils.isNotBlank(address) ? address : car.getAddress());
      car.setRidepeopleNumber(
          ridepeopleNumber != null ? ridepeopleNumber : car.getRidepeopleNumber());
      car.setLines(lines != null && lines.size() > 0 ? lines : car.getLines());
      
      car.setPlateColor(plateColor);
      car.setBrand(brand);
      car.setVehicleType(vehicleType);
      car.setVehicleModel(vehicleModel);
      car.setOwner(owner);
      car.setBodyColor(bodyColor);
      car.setEngineNo(engineNo);
      car.setVin(vin);
      car.setFuel(fuel);
      car.setPermitted(permitted);
      car.setInsurer(insurer);
      car.setInsuranceNo(insuranceNo);
      car.setInsType(insType);
      car.setInsMoney(insMoney);
      if (null != insValidTime) {
        car.setInsValidTime(new Date(insValidTime));
      }
      if (null != registrationTime) {
        car.setRegistrationTime(new Date(registrationTime));
      }
      car.setInspectioStatus(inspectioStatus);
      car.setRateType(rateType);
      car.setDisplacement(displacement);
      car.setMaintenanceStatus(maintenanceStatus);
      car.setTaxiNo(taxiNo);
      car.setTaxiAgency(taxiAgency);
      car.setTaxiRegion(taxiRegion);
      if (null != validTime) {
        car.setValidTime(new Date(validTime));
      }
      if (null != invalidTime) {
        car.setInvalidTime(new Date(invalidTime));
      }
      if (null != taxiRegisterTime) {
        car.setTaxiRegisterTime(new Date(taxiRegisterTime));
      }
      car.setDeviceSerial(deviceSerial);
      car.setDeviceBrand(deviceBrand);
      car.setDeviceModel(deviceModel);
      car.setAddress(address);
      if (null != deviceInstallTime) {
        car.setDeviceInstallTime(new Date(deviceInstallTime));
      }
      carManager.update(car);
      
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
     
      return response;
    } catch (Exception ex) {
      logger.error("update:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    
  }
  
  /**
   * 根据车辆id和appKey查询单个车辆 .
   * @param carId  车辆id
   * @param appKey 区分系统
   * 
   */
  @RequestMapping("findOne")
  @ResponseBody
  public ResultTransport findOne(
      @RequestParam("carId") Long carId,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      Car car = carManager.findOne(carId, appKey);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(car);
      return response;
    } catch (Exception ex) {
      logger.error("findOne:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }

  }
  
  /**
   * 根据车牌号和appKey查询车辆 .
   * @param carNumber  车牌号
   * @param appKey     区分系统
   * 
   */
  @RequestMapping("findBycarNumber")
  @ResponseBody
  public ResultTransport findBycarNumber(
      @RequestParam("carNumber") String carNumber,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      List<Car> car = carManager.findBycarNumber(carNumber, appKey);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(car);
      return response;
    } catch (Exception ex) {
      logger.error("findByCarNumber:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }

  }
  /**
   * 根据车牌号查询车辆.
   */
  @RequestMapping("queryCarByNumber")
  @ResponseBody
  public ResultTransport queryCarByNumber(
      @RequestParam("carNumber") String carNumber,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      Car car = carManager.queryCarByNumber(carNumber, appKey);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(car);
      return response;
    } catch (Exception ex) {
      logger.error("findByCarNumber:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    
  }
  
  /**
   * 多条件分页查询车辆 .
   * @param lineId          线路id
   * @param lineName        线路名称
   * @param companyId       公司id
   * @param companyName     公司名称
   * @param carName         车辆名称
   * @param carNumber       车牌号
   * @param status          车辆状态
   * @param employId        司机id
   * @param employName      司机名称
   * @param page            页数(从1开始)
   * @param rows            每页数量
   * @param appKey          区分系统
   * 
   */
  @RequestMapping("findByParam")
  @ResponseBody
  public ResultTransport findByParam(
      @RequestParam(required = false, value = "lineId") Long lineId,
      @RequestParam(required = false, value = "lineName") String lineName, 
      @RequestParam(required = false, value = "companyId") Long companyId, 
      @RequestParam(required = false, value = "companyName") String companyName, 
      @RequestParam(required = false, value = "carName") String carName,
      @RequestParam(required = false, value = "carNumber") String carNumber, 
      @RequestParam(required = false, value = "statuss") Integer[] status, 
      @RequestParam(required = false, value = "employId") Long employId,
      @RequestParam(required = false, value = "employName") String employName, 
      @RequestParam(required = false, value = "page") Integer page, 
      @RequestParam(required = false,value = "rows") Integer rows,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      DataGrid<Car> cars =
          carManager.findByParam(lineId, lineName, companyId, companyName, carName, carNumber,
              status, employId, employName, page, rows, appKey);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(cars);
      return response;
    } catch (Exception ex) {
      logger.error("findByParam:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }

  }
  
  /**
   * 删除车辆 .
   * @param carId   车辆id
   * @param appKey  区分系统
   */
  @RequestMapping("delete")
  @ResponseBody
  public ResultTransport delete(@RequestParam("carId") Long carId, @RequestParam("lineId") Long[] lineId, @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    try {
      carManager.delete(carId, lineId, appKey);
//        carManager.delete(carId, appKey);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return response;
    } catch (Exception ex) {
      logger.error("delete:{}",ex.getMessage());
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }

  }


}
